<?
/*
*	: LI-NA (hyung778@gmail.com);
*	̼: GPL v2;
*	γ: ̽Ű;
*/
	if($called_position!='before_module_proc') return; //XE ƹ  ֵ  ȣ  
	$logged_info = Context::get('logged_info'); // ȣ  logged_info ⺻ Ƿ  .
	if($logged_info->is_admin == 'Y') return; //  
	if(!$addon_info->apikey) return; //APIŰ   
	session_start();
	
	exit($_SESSION['checkkorean'].' '.$_SERVER['REMOTE_ADDR']);

function accessDenide($msg) {
	if(!$msg) $msg = 'Access Denied.';
	header("Content-Type: text/html; charset=UTF-8");
	header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
	header("Cache-Control: no-store, no-cache, must-revalidate");
	header("Cache-Control: post-check=0, pre-check=0", false);
	header("Pragma: no-cache");
	header("Set-Cookie: ");
	exit('<!DOCTYPE html><html><head><meta charset="utf-8"><title>Access Denied.</title><style>*{padding:0;margin:0}</style></head><body><div style="width:400px;margin:30px auto;background-color:#ffffff;border:1px solid #999;border-radius: 6px;box-shadow:0 3px 7px #ccc"><div style="padding:9px 15px;font-size:16px;line-height:1.4;font-weight:600;color:#666;font-family:,NanumGothic,;">'.$msg.'</div></div></body></html>');
} //  

function checkReturn($value) {
	switch($value) {
		case 'true':	$_SESSION['checkkorean'] = 'true';
						break;
		case 'false':	$_SESSION['checkkorean'] = 'false';
						accessDenide();
						break;
		case 'error':	$error_msg1 = '  ߻߽ϴ.';
						$error_msg2 = $buffer;;
						$_SESSION['checkkorean'] = 'error';
						if($addon_info->debug == 'Y' || $addon_info->errormode == 'Y') {
							header("Content-Type: text/html; charset=UTF-8");
							header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
							header("Cache-Control: no-store, no-cache, must-revalidate");
							header("Cache-Control: post-check=0, pre-check=0", false);
							header("Pragma: no-cache");
							header("Set-Cookie: ");
							exit('<!DOCTYPE html><html><head><meta charset="utf-8"><title>Debug Mode</title><style>*{padding:0;margin:0}</style></head><body><div style="width:400px;margin:30px auto;background-color:#ffffff;border:1px solid #999;border-radius: 6px;box-shadow:0 3px 7px #ccc"><div style="padding:9px 15px;font-size:16px;line-height:1.4;font-weight:600;color:#666;font-family:,NanumGothic,;"><h2>'.$error_msg1.'</h2><br><br>'.$error_msg2.'</div></div></body></html>');
						}else debugPrint($error_msg1.' error msg: '.$error_msg2);
						break;
		case 'limit':	debugPrints('api ȣ Ƚ Դϴ.',$buffer);
						break;
		default:		debugPrints('ϰ ߸Ǿϴ.',$buffer);
						break;
	}
} //API ϵǴ° ˻ 
	
		
	if($_SESSION['checkkorean'] == 'false') accessDenide(); //̹ ѱ ǰ ƴ϶ ˻
	else if($_SESSION['checkkorean'] == 'true') return; //ѱ ϶
	else if($_SESSION['checkkorean'] == 'error' && $addon_info->errormode == 'Y') accessDenide('Access Denied (server error).'); //   addon_info 忡    ȣ ʰ ٷ 
	
	
	$ip = $_SERVER['REMOTE_ADDR']; //ip
	
	if($addon_info->blockip) {
		$blockip = explode("\n",$addon_info->blockip);
		foreach($blockip as $key => $value) {
			$blockip[$key] = explode(",",$value);
			$ip2_from = ip2long($blockip[$key][0]);
			$ip2_to = ip2long($blockip[$key][1]);
			$ip2 = ip2long($ip);
			if($ip_from <= $ip2 && $ip_to >= $ip2) {
				$_SESSION['checkkorean'] = 'false';
				accessDenide();
			}
		}
	}
	
	if($addon_info->gettype == 'https') $url = 'https://ip.atde.kr/?api='.$addon_info->apikey.'&ip='.$ip;
	else $url = 'http://ip.atde.kr/?api='.$addon_info->apikey.'&ip='.$ip;
	$agent = 'Mozilla/5.0 (compatible; XE/'.__XE_VERSION__.')';
	
	
	$curlsession = curl_init (); //https   curl  
	curl_setopt($curlsession, CURLOPT_URL, $url);
	curl_setopt($curlsession, CURLOPT_HEADER, false);
	curl_setopt($curlsession, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($curlsession, CURLOPT_POST, false);
	curl_setopt($curlsession, CURLOPT_USERAGENT, $agent);
	curl_setopt($curlsession, CURLOPT_REFERER, "");
	curl_setopt($curlsession, CURLOPT_TIMEOUT, 3);
	curl_setopt($curlsession,CURLOPT_SSL_VERIFYPEER, false);
	$buffer = curl_exec($curlsession);
	$cinfo = curl_getinfo($curlsession);
	curl_close($curlsession);
	if ($cinfo['http_code'] != 200) {
		checkReturn($buffer);
	}else {
		$error_msg1 = 'API ȣ  ߻߽ϴ.';
		$error_msg2 = $cinfo['http_code'].'<br>'.curl_error($curlsession).'<br>'.$buffer;
		$_SESSION['checkkorean'] = 'error';
		if($addon_info->debug == 'Y' || $addon_info->errormode == 'Y') {
			header("Content-Type: text/html; charset=UTF-8");
			header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
			header("Cache-Control: no-store, no-cache, must-revalidate");
			header("Cache-Control: post-check=0, pre-check=0", false);
			header("Pragma: no-cache");
			header("Set-Cookie: ");
			exit('<!DOCTYPE html><html><head><meta charset="utf-8"><title>Debug Mode</title><style>*{padding:0;margin:0}</style></head><body><div style="width:400px;margin:30px auto;background-color:#ffffff;border:1px solid #999;border-radius: 6px;box-shadow:0 3px 7px #ccc"><div style="padding:9px 15px;font-size:16px;line-height:1.4;font-weight:600;color:#666;font-family:,NanumGothic,;"><h2>'.$error_msg1.'</h2><br><br>'.$error_msg2.'</div></div></body></html>');
		}else debugPrint($error_msg1.' error msg: '.$error_msg2);
	}
?>